package com.fafa.pos.dao.impl;

import android.content.ContentValues;
import android.database.Cursor;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IVipInfoDao;
import com.fafa.pos.pojo.CardInfo;
import com.fafa.pos.pojo.UserInfo;
import com.fafa.pos.pojo.VipGroupInfo;
import com.fafa.pos.pojo.VipInfo;
import com.fafa.pos.util.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ZhouBing on 2017/8/10.
 *
 * SQlite -- T_VIPINFO
 */
public class VipInfoDaoImpl implements IVipInfoDao{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private DBUtil dbUtil;
    private ConfigDaoImpl configDao;

    public VipInfoDaoImpl(DBUtil dbUtil) {
        this.dbUtil = dbUtil;
        configDao = new ConfigDaoImpl(dbUtil);
    }

    @Override
    public void saveVipInfoList(List<VipInfo> vipInfos) {

        dbUtil.getDB().beginTransaction();
        String updateDate = "1900-00-00 00:00:00.000";
        try{
            for (VipInfo vipInfo:vipInfos) {

                //Id存在则更新不存在新增
                String sql = "select VIP_ID from T_VIPINFO where VIP_ID = ?";
                Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{vipInfo.getVipId()});

                ContentValues contentValues = new ContentValues();
                contentValues.put("VIP_ID",vipInfo.getVipId());
                contentValues.put("VIP_CODE",vipInfo.getVipCode());
                contentValues.put("VIP_NAME",vipInfo.getVipName());
                contentValues.put("VIP_STATUS",vipInfo.getVipStatus());
                contentValues.put("GROUP_ID",vipInfo.getGroupId());
                contentValues.put("VISIBLE",vipInfo.getVisible());
                contentValues.put("UPDATE_DATE",vipInfo.getUpdate());

                Date tempDate = sdf.parse(vipInfo.getUpdate());

                if (sdf.parse(updateDate).before(tempDate)) {
                    updateDate=sdf.format(tempDate);
                }

                if(cursor.moveToNext()){
                    dbUtil.getDB().update("T_VIPINFO",contentValues," VIP_ID = ?",new String[]{vipInfo.getVipId()});
                }else{
                    dbUtil.getDB().insert("T_VIPINFO",null,contentValues);
                }
                if(cursor!=null){
                    cursor.close();
                }
            }
            configDao.saveItem("T_VIPINFO_UPDATE_DATE", updateDate);
            dbUtil.getDB().setTransactionSuccessful();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            dbUtil.getDB().endTransaction();
            dbUtil.close();
        }
    }

    @Override
    public VipInfo getVipInfo(String vipId) {

        String sql = "select * from T_VIPINFO where VIP_ID = ?";
        Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{vipId});
        if(cursor.moveToNext()){
            VipInfo vipInfo = new VipInfo();
            vipInfo.setVipId(cursor.getString(0));
            vipInfo.setVipCode(cursor.getString(1));
            vipInfo.setVipName(cursor.getString(2));
            vipInfo.setVipStatus(cursor.getString(3));
            vipInfo.setGroupId(cursor.getString(4));
            vipInfo.setVisible(cursor.getString(5));
            vipInfo.setUpdate(cursor.getString(6));
            return  vipInfo;
        }
        if(cursor!=null){
            cursor.close();
        }
        dbUtil.close();
        return null;
    }
}
